package com.yellowpage.bigdata.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.map.LinkedMap;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import com.opensymphony.xwork2.ActionContext;
import com.yellowpage.white.common.Constants;
import com.yellowpage.white.common.PageRender;
import com.yellowpage.white.dao.QueryAssistantSQL;
import com.yellowpage.white.service.BaseServiceImpl;
import com.yellowpage.bigdata.action.NspFatallogImeiWeekregionSumAction;
import com.yellowpage.bigdata.data.NspFatallogImeiWeekregionSum;
import com.yellowpage.bigdata.dao.NspFatallogImeiWeekregionSumDao;
import com.yellowpage.ca.data.User;
import com.yellowpage.ca.service.RegionService;

public class NspFatallogImeiWeekregionSumServiceImpl extends BaseServiceImpl
		implements NspFatallogImeiWeekregionSumService {
	private NspFatallogImeiWeekregionSumDao nspFatallogImeiWeekregionSumDao;
	public void setNspFatallogImeiWeekregionSumDao(
			NspFatallogImeiWeekregionSumDao nspFatallogImeiWeekregionSumDao) {
		this.nspFatallogImeiWeekregionSumDao = nspFatallogImeiWeekregionSumDao;
	}

	/* Smooth generate Method */
	public NspFatallogImeiWeekregionSum getNspFatallogImeiWeekregionSum(Long id) {
		return nspFatallogImeiWeekregionSumDao.get(id);
	}

	public List<NspFatallogImeiWeekregionSum> getAllNspFatallogImeiWeekregionSum() {
		return nspFatallogImeiWeekregionSumDao.getAll();
	}
	
	public List<Map> queryNspFatallogImeiWeekregionSumByPage(Map queryMap,
			PageRender pageRender) {
		String t1 = queryMap.get("requestType").equals("") ? "" : " and REQUEST_TYPE in "+queryMap.get("requestType");
		String t2 = queryMap.get("regionCode").equals("") ? "" : " and REGION_CODE in "+queryMap.get("regionCode");
		String sql1="";
		if(NspFatallogImeiWeekregionSumAction.isType){
			sql1="SELECT a.REQUEST_TYPE, a.REGION_CODE , a.LOG_WEEK , a.IMEI_WEEK_COUNT , a.IMEI_NEW_COUNT , a.IMEI_ALL_COUNT ,a.TOTAL ,a.HIMEI ,a.NOIMEI , "
					+ " ( SELECT c.COMPANY  FROM NSP_REQUEST_TYPE c ,NSP_CHANNEL d where c.code=a.REQUEST_TYPE AND c.CHANNEL_CODE=d.code ) as REQUEST_NAME, "
					+ " ( SELECT d.name  FROM NSP_CHANNEL d where a.REQUEST_TYPE =d.code ) as CHANNEL, "
					+ "(select c.name from OMC_REGION c where c.REGION_CODE=a.REGION_CODE) as REGION_NAME "
					+ "  FROM NSP_FATALLOG_IMEI_WEEKREGION_SUM a WHERE 1=1 " + t1 + t2;
		}else{
			sql1="SELECT a.REQUEST_TYPE, a.REGION_CODE , a.LOG_WEEK , a.IMEI_WEEK_COUNT , a.IMEI_NEW_COUNT , a.IMEI_ALL_COUNT ,a.TOTAL ,a.HIMEI ,a.NOIMEI , "
					+ " ( SELECT c.COMPANY  FROM NSP_REQUEST_TYPE c ,NSP_CHANNEL d where c.code=a.REQUEST_TYPE AND c.CHANNEL_CODE=d.code ) as REQUEST_NAME, "
					+ " ( SELECT d.name  FROM NSP_REQUEST_TYPE c ,NSP_CHANNEL d where c.code=a.REQUEST_TYPE AND c.CHANNEL_CODE=d.code ) as CHANNEL, "
					+ "(select c.name from OMC_REGION c where c.REGION_CODE=a.REGION_CODE) as REGION_NAME "
					+ "  FROM NSP_FATALLOG_IMEI_WEEKREGION_SUM a WHERE 1=1 " + t1 + t2;
		}
		QueryAssistantSQL sqlAssistant = baseDao
				.newQueryAssistantSQL(sql1)
				.addCondition(" and  LOG_WEEK >= :logWeek ")
				.addCondition(" and  LOG_WEEK <= :logWeek_end ");
			List<Map> resultList = sqlAssistant.queryByPage(
				baseDao.getConnection(), queryMap, pageRender);
		return resultList;
	}

	public void saveNspFatallogImeiWeekregionSum(
			NspFatallogImeiWeekregionSum nspFatallogImeiWeekregionSum) {
		nspFatallogImeiWeekregionSumDao.save(nspFatallogImeiWeekregionSum);
	}

	public void updateNspFatallogImeiWeekregionSum(
			NspFatallogImeiWeekregionSum nspFatallogImeiWeekregionSum) {
		nspFatallogImeiWeekregionSumDao.update(nspFatallogImeiWeekregionSum);
	}

	public void delete(List deleteList) {
		nspFatallogImeiWeekregionSumDao.remove(deleteList);
	}
	@Override
	public Map<String,String> getRegionMap(String regionSql) {
		Map<String,String> dataSourceMap = new LinkedMap();
		QueryAssistantSQL sqlAssistant = baseDao
				.newQueryAssistantSQL(" select a.REGION_CODE as NAME, a.SHORT_NAME as VALUE from OMC_REGION a where 1 = 1 "+ regionSql);
		List<Map> resultList = sqlAssistant.query(baseDao.getConnection(),
				new HashMap());
		for (int i = 0; i < resultList.size(); i++) {
			dataSourceMap.put(resultList.get(i).get("NAME").toString(), resultList.get(i).get("VALUE").toString());
		}
		return dataSourceMap;
	}
	
 	@Override
	public Map<String,String> getChannelMap() {
		Map<String,String> dataSourceMap = new LinkedMap();
		QueryAssistantSQL sqlAssistant = baseDao
				.newQueryAssistantSQL(" SELECT a.code as NAME, a.name as VALUE  FROM NSP_CHANNEL a  ");
		List<Map> resultList = sqlAssistant.query(baseDao.getConnection(),
				new HashMap());
		for (int i = 0; i < resultList.size(); i++) {
			dataSourceMap.put(resultList.get(i).get("NAME").toString(), resultList.get(i).get("VALUE").toString());
		}
		return dataSourceMap;
	}
 	@Override
	public Map<String,String> getrequestTypeMap() {
		Map<String,String> dataSourceMap = new LinkedMap();
		QueryAssistantSQL sqlAssistant = baseDao
				.newQueryAssistantSQL(" SELECT a.COMPANY as NAME, a.CODE as VALUE  FROM NSP_REQUEST_TYPE a  ");
		sqlAssistant.setTail("order by (a.CODE+0)");
		List<Map> resultList = sqlAssistant.query(baseDao.getConnection(),
				new HashMap());
		for (int i = 0; i < resultList.size(); i++) {
			dataSourceMap.put(resultList.get(i).get("VALUE").toString(), resultList.get(i)
					.get("VALUE") + "-" + resultList.get(i).get("NAME"));
		}
		return dataSourceMap;
	}

 	@Override
	public String getrequestTypeString(String query_requestType) {
		String val = "";
		QueryAssistantSQL sqlAssistant = baseDao
				.newQueryAssistantSQL(" select c.`CODE` as NAME from NSP_REQUEST_TYPE c where c.channel_code in ("+query_requestType+") ");
		List<Map> resultList = sqlAssistant.query(baseDao.getConnection(),
				new HashMap());
		for (int i = 0; i < resultList.size(); i++) {
		    val += "'" + resultList.get(i).get("NAME").toString()+ "'," ;
		}
		return val.substring(0, val.length() - 1);
	}


}